﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Xml;
using SHomeWorkshop.LunarPresentation.Widegets;

namespace SHomeWorkshop.LunarPresentation.Expressions
{
    public class Arrow : Expression
    {
        public Arrow(SubPanel rootPanel, SubPanel parentPanel, Editor editor)
            : base(rootPanel, parentPanel, editor)
        {
            aPanel = new ArrowPanel(this);
            if (parentPanel != null && parentPanel.Direction == Orientation.Horizontal)
            {
                aPanel.MinHeight = 40;
                aPanel.MinWidth = 20;
                aPanel.Width = 20;
                aPanel.Height = double.NaN;
            }
            else
            {
                aPanel.MinHeight = 20;
                aPanel.MinWidth = 40;
                aPanel.Height = 20;
                aPanel.Width = double.NaN;
            }
            aPanel.Foreground = ExForeground;

            aPanel.MouseDown += new System.Windows.Input.MouseButtonEventHandler(aPanel_MouseDown);
            aPanel.MouseLeftButtonUp += new System.Windows.Input.MouseButtonEventHandler(aPanel_MouseLeftButtonUp);

            aPanel.HorizontalAlignment = HorizontalAlignment.Stretch;
            aPanel.VerticalAlignment = VerticalAlignment.Stretch;
            this.mainBorder.Child = aPanel;
            this.mainBorder.Background = Brushes.Transparent;
            this.HorizontalAlignment = HorizontalAlignment.Stretch;
            this.VerticalAlignment = VerticalAlignment.Stretch;

            this.chineseName = "箭头";
        }

        void aPanel_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            // 接收拖动来的公式。
            Globals.DropExpression(this);
        }

        void aPanel_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (Editor == null) return;
            Editor.SelectedExpression = this;

            //准备拖动公式。
            Globals.dragSourceExpression = this;
            e.Handled = true;
        }

        private ArrowPanel aPanel;

        public ArrowPanel APanel
        {
            get { return aPanel; }
        }

        public override void UpdateAlignment()
        {
            if (ParentPanel != null)
            {
                if (ParentPanel.Direction == Orientation.Horizontal)
                {
                    if (aPanel != null)
                    {
                        aPanel.MinHeight = 40;
                        aPanel.MinWidth = 20;
                        aPanel.Width = 20;
                        aPanel.Height = double.NaN;
                    }
                    switch (align)
                    {
                        case Alignment.LeftOrTop:
                            {
                                this.VerticalAlignment = VerticalAlignment.Top; break;
                            }
                        case Alignment.Center:
                            {
                                this.VerticalAlignment = VerticalAlignment.Center; break;
                            }
                        case Alignment.RightOrBottom:
                            {
                                this.VerticalAlignment = VerticalAlignment.Bottom; break;
                            }
                        default:
                            {
                                this.VerticalAlignment = VerticalAlignment.Stretch; break;
                                // 水平子面板中的公式，默认两端对齐。
                            }
                    }
                }
                else if (ParentPanel.Direction == Orientation.Vertical)
                {
                    if (aPanel != null)
                    {
                        aPanel.MinWidth = 40;
                        aPanel.MinHeight = 20;
                        aPanel.Height = 20;
                        aPanel.Width = double.NaN;
                    }
                    switch (align)
                    {
                        case Alignment.LeftOrTop:
                            {
                                this.HorizontalAlignment = HorizontalAlignment.Left; break;
                            }
                        case Alignment.Center:
                            {
                                this.HorizontalAlignment = HorizontalAlignment.Center; break;
                            }
                        case Alignment.RightOrBottom:
                            {
                                this.HorizontalAlignment = HorizontalAlignment.Right; break;
                            }
                        default:
                            {
                                this.HorizontalAlignment = HorizontalAlignment.Stretch; break;
                                // 垂直子面板中的公式，默认两端对齐。
                            }
                    }
                }
            }

            if (aPanel != null)
            {
                aPanel.UpdateArrows();
            }
        }

        private ArrowLocation arrows = ArrowLocation.All;

        public ArrowLocation Arrows
        {
            get { return arrows; }
            set
            {
                arrows = value;
                this.XmlData.SetAttribute("Arrows", arrows.ToString());
                UpdateAlignment();

                if (Editor != null)
                {
                    Editor.IsModified = true;
                }
            }
        }

        public override Brush ExForeground
        {
            get
            {
                return base.ExForeground;
            }
            set
            {
                UpdateExForegroundWithoutXmlChanging(value);
                UpdateExForegroundToXmlData(value);
                Editor.IsModified = true;
            }
        }

        public override void UpdateExForegroundWithoutXmlChanging(Brush value)
        {
            base.UpdateExForegroundWithoutXmlChanging(value);

            base.exForeground = value;
            this.aPanel.Foreground = value;
        }

        protected override void BuildChildren()
        {
            base.BuildChildren();

            if (this.XmlData == null) return;

            XmlAttribute attrArrows = XmlData.GetAttribute("Arrows");
            if (attrArrows != null)
            {
                this.arrows = (ArrowLocation)Enum.Parse(typeof(ArrowLocation), attrArrows.Value);
            }
        }

        public enum ArrowLocation { Start, End, All, None }

        public override void SetProperty(string propertyName, string value)
        {
            base.SetProperty(propertyName, value);

            switch (propertyName)
            {
                case "Arrows":
                    {
                        this.Arrows = (ArrowLocation)Enum.Parse(typeof(ArrowLocation), value);
                        break;
                    }
            }
        }
    }
}
